package com.shoppingwebsite.controller;

import com.shoppingwebsite.domain.BaseContext;
import com.shoppingwebsite.domain.YtGoods;
import com.shoppingwebsite.domain.Result;
import com.shoppingwebsite.domain.YtUser;
import com.shoppingwebsite.service.GoodsService;
import com.shoppingwebsite.service.OrderService;
import com.shoppingwebsite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    //得到所有商品
    @GetMapping("/getAll")
    public List<YtGoods> registerJudge(){
        //获得所有商品信息
        List<YtGoods> goods = goodsService.selectAll();
        //返回结果集
        return goods;
    }

    //根据名称查找商品
    @GetMapping("/search")
    public List<YtGoods> search(String name){
        //避免中文乱码
        try {
            name = new String(name.getBytes("iso8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //模糊查询
        name = "%"+name+"%";

        //获得所有商品信息
        System.out.println(name);
        List<YtGoods> goods = goodsService.selectByName(name);
        System.out.println(goods.toString());

        //返回结果集
        return goods;
    }

    //销量排序
    @GetMapping("/sortSearch")
    public List<YtGoods> sortSearch(String sort){
    //避免中文乱码
        try {
            sort = new String(sort.getBytes("iso8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        List<YtGoods> goodsList;
        //判断 销量/价格 高低
        if(sort.equals("热门↑")){
            goodsList = goodsService.search("xl asc");
        }else if(sort.equals("热门↓")){
            goodsList = goodsService.search("xl desc");
        }else if(sort.equals("价格↑")){
            goodsList = goodsService.search("price asc");
        }else {
            goodsList = goodsService.search("price desc");
        }

        return goodsList;
    }

    //购买商品
    @PostMapping("/buy")
    public Result buy(String names,String priceAlls){
        //获得总金额
        String[] priceAlls2 = priceAlls.split(",");
        double priceAll = 0;
        for(int i=0;i<priceAlls2.length;i++){
            priceAll += Double.parseDouble(priceAlls2[i].substring(1));
        }

        Result result=new Result();
        try {
            //购买商品
            goodsService.buyGoods(names);
            //根据用户id获得用户
            Integer userId = BaseContext.getCurrentId();
            YtUser user = userService.selectById(userId);
            //添加订单
            orderService.add(user,names,priceAll);
        } catch (Exception e) {
            result.setFlag(false);
            result.setInfo("购买失败");
            return result;
        }

        result.setFlag(true);
        result.setInfo("购买成功");
        return result;
    }
}
